"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : cm_install
Case Name   : cm_install不指定-X参数
Create At   : 2022/11/15
Owner       : opentestcase012
Description :
    1: 数据库用户下执行cm_install,不指定-X参数;
    2: gs_om查看集群状态;
    3: cm_ctl查看集群状态;
    4: 查看数据库用户下进程和定时任务;
Expect      :
    1: 安装失败
    2: 数据库状态正常,未安装cm
    3: 失败,未安装cm
    4: 无om_monitor、cm_agent、cm_server的进程，无定时任务
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.cm_tool = os.path.join(macro.DB_INSTANCE_PATH, '..', 'app',
                                    'tool', 'cm_tool')

    def test_standby(self):
        self.log.info('-----查询系统和数据库版本-----')
        os_cpu = self.pri_root.sh('cat /etc/*release;'
                                  'uname -a').result()
        self.log.info(os_cpu)
        find_cmd = f'source {macro.DB_ENV_PATH};' \
            f'gaussdb -V'
        find_ver_msg = self.common.get_sh_result(self.pri_user, find_cmd)
        assert find_ver_msg.find('compiled at') > -1
        db_release_version = find_ver_msg.split()[2]

        self.log.info('-----获取cm压缩包路径-----')
        cm_tgz = ''
        if 'CentOS Linux 7' in os_cpu:
            cm_tgz = f'openGauss-{db_release_version}-CentOS-64bit-cm.tar.gz'
        elif 'openEuler 20.03' in os_cpu:
            cm_tgz = f'openGauss-{db_release_version}-openEuler-64bit-cm.tar.gz'
        cmpkg = os.path.join(os.path.dirname(macro.DB_SCRIPT_PATH), cm_tgz)

        text = '-----step1:数据库用户下执行cm_install,不指定-X参数 expect:安装失败-----'
        self.log.info(text)
        install_cmd = f'cd {self.cm_tool};' \
            f'./cm_install -e {macro.DB_ENV_PATH} ' \
            f'--cmpkg {cmpkg}'
        self.log.info(install_cmd)
        install_res = self.common.get_sh_result(self.pri_user, install_cmd)
        error_msg = "[GAUSS-50001] : Incorrect parameter. " \
                    "Parameter '-X' is required."
        self.assertIn(error_msg, install_res, '执行失败:' + text)

        text3 = '-----step2:gs_om查看集群状态;expect: 数据库状态正常,未安装cm-----'
        self.log.info(text3)
        status_msg = Primary_SH.get_db_cluster_status('detail')
        self.log.info(status_msg)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_msg,
                      "执行失败:" + text)
        self.assertNotIn('CMServer State', status_msg, "执行失败:" + text)

        text = '-----step3:cm_ctl查看集群状态 expect:失败,未安装cm-----'
        self.log.info(text)
        show_status = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl query -Civdp'
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn("can't connect to cm_server", status_msg, "执行失败:" + text)

        text = '----step4:查看数据库用户下进程和定时任务 ' \
               'expect:无om_monitor、cm_agent、cm_server的进程，无定时任务----'
        self.log.info(text)
        show_cmd = "crontab -l;ps ux|egrep 'cm_server|cm_agent|om_monitor';"
        process_res = self.primary_sh.exec_gs_ssh(show_cmd)
        self.assertNotIn(process_res, 'bin/cm_server', "执行失败:" + text)
        self.assertNotIn(process_res, 'bin/cm_agent', "执行失败:" + text)
        self.assertNotIn(process_res, 'om_monitor -L', "执行失败:" + text)

    def tearDown(self):
        self.log.info('----无需清理环境----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
